set more off
clear all

version 17.0
set matsize 10000

global xlist linccap lsize housing_nrooms lagehead lyeduc cook_jobweeks lyeduccook housing_selfowned sexhead agrihead unemphead cook_female d_road elec rural i.religion i.ethnicgroup //i.location
global xlisturban linccap lsize housing_nrooms lagehead lyeduc cook_jobweeks lyeduccook housing_selfowned sexhead agrihead unemphead cook_female i.religion i.ethnicgroup //i.location
global reglist linccap lsize housing_nrooms lagehead lyeduc cook_jobweeks 
global absorblist housing_selfowned sexhead agrihead unemphead cook_female d_road elec location religion ethnicgroup 
global absorblisturban housing_selfowned sexhead agrihead unemphead cook_female location religion ethnicgroup  
global absorblistDID housing_selfowned sexhead agrihead unemphead cook_female d_road elec location religion ethnicgroup
global absorblisturbanDID housing_selfowned sexhead agrihead unemphead cook_female location religion ethnicgroup


************************************************************************************************
*** Robustness ****************************************
************************************************************************************************

*** Table A1: Survey sequence ************************************************************************

****** rural ******
use "$datain\glss_DiDsample.dta", clear
cd "$output"
keep if (glss==6 & (aftercategory==2 | aftercategory==3)) | (glss==5 & eligible5==1)
*keep if earlycategory==1 & rural==1 & (glss==5 | glss==6)
keep if late<0.9 & rural==1
keep if cooking!=0
drop if cooking==.

*replace treat=latetreat


gen tag=0
levelsof location, local(temp)
foreach k of local temp{
	sum treat if location==`k'
	replace tag=1 if location==`k' & (r(mean)==1 | r(mean)==0)
}

ebalance treat $xlist if tag==0 & glss==5, keep(tempbalance) replace //doesnt work
gen weight_cater=_webal

ebalance treat $xlist if tag==0 & glss==6, keep(tempbalance) replace 
replace weight_cater=_webal if glss==6

reghdfe d_wood treat did $reglist [aweight=weight_cater] if rural==1, absorb($absorblistDID ) vce(cluster clustervar)
est store main1r
reghdfe d_charcoal treat did $reglist [aweight=weight_cater] if rural==1, absorb($absorblistDID ) vce(cluster clustervar)
est store main2r
reghdfe d_lpg treat did $reglist [aweight=weight_cater] if rural==1, absorb($absorblistDID ) vce(cluster clustervar)
est store main3r

reghdfe lq_coal_hat treat did $reglist [aweight=weight_cater] if rural==1, absorb($absorblistDID ) vce(cluster clustervar)
est store coal1r
reghdfe d_qcoal treat did $reglist [aweight=weight_cater] if rural==1, absorb($absorblistDID ) vce(cluster clustervar)
est store coal2r

reghdfe lexp_lpg treat did $reglist [aweight=weight_cater] if rural==1, absorb($absorblistDID ) vce(cluster clustervar)
est store lpg1r
reghdfe d_elpg treat did $reglist [aweight=weight_cater] if rural==1, absorb($absorblistDID ) vce(cluster clustervar)
est store lpg2r

*** tables
local filename table_a1_robseq_did_cattr
est restore main1r
outreg2 using "$output\\`filename'.tex", replace tex(fragment) label dec(3) adjr2
est restore main2r
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore main3r
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore coal1r
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore coal2r
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore lpg1r
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore lpg2r
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2


****** urban ******
use "$datain\glss_DiDsample.dta", clear
cd "$output"
keep if (glss==6 & (aftercategory==2 | aftercategory==3)) | (glss==5 & eligible5==1)
keep if late<0.9 & rural==0
keep if cooking!=0
drop if cooking==.

*** weights
//main
gen tag=0
levelsof location, local(temp)
foreach k of local temp{
	sum treat if location==`k'
	replace tag=1 if location==`k' & (r(mean)==1 | r(mean)==0)
}

ebalance treat $xlisturban if tag==0 & glss==5, keep(tempbalance) replace 
gen weight_cateu=_webal
ebalance treat $xlisturban if tag==0 & glss==6, keep(tempbalance) replace 
replace weight_cateu=_webal if glss==6

//coal intensive
cap drop tag2
gen tag2=0
levelsof location, local(temp)
foreach k of local temp{
	foreach l in 0 1{
		foreach t in 0 1{
			qui sum treat if location==`k' & d_road==`l' & elec==`t' & lq_coal_hat2!=.
			qui replace tag2=1 if location==`k' & d_road==`l' & elec==`t' & lq_coal_hat2!=. & (r(mean)==1 | r(mean)==0)
		}
	}
}
ebalance treat $xlisturban if tag2==0 & glss==5 & lq_coal_hat2!=., keep(tempbalance) replace 
gen weight_cateu_coal=_webal
ebalance treat $xlisturban if tag2==0 & glss==6 & lq_coal_hat2!=., keep(tempbalance) replace 
replace weight_cateu_coal=_webal if glss==6

//lpg intensive
cap drop tag3
gen tag3=0
levelsof location, local(temp)
foreach k of local temp{
	foreach l in 0 1{
		foreach t in 0 1{
			qui sum treat if location==`k' & d_road==`l' & elec==`t' & lexp_lpg2!=.
			qui replace tag3=1 if location==`k' & d_road==`l' & elec==`t' & lexp_lpg2!=. & (r(mean)==1 | r(mean)==0)
		}
	}
}
ebalance treat $xlisturban if tag3==0 & glss==5 & lexp_lpg2!=., keep(tempbalance) replace 
gen weight_cateu_lpg=_webal
ebalance treat $xlisturban if tag3==0 & glss==6 & lexp_lpg2!=., keep(tempbalance) replace 
replace weight_cateu_lpg=_webal if glss==6


*** regressions
reghdfe d_wood treat did $reglist [aweight=weight_cateu] if rural==0, absorb($absorblisturbanDID ) vce(cluster clustervar)
est store main1u
reghdfe d_charcoal treat did $reglist [aweight=weight_cateu] if rural==0, absorb($absorblisturbanDID ) vce(cluster clustervar)
est store main2u
reghdfe d_lpg treat did $reglist [aweight=weight_cateu] if rural==0, absorb($absorblisturbanDID ) vce(cluster clustervar)
est store main3u

reghdfe lq_coal_hat treat did $reglist [aweight=weight_cateu] if rural==0, absorb($absorblisturbanDID ) vce(cluster clustervar)
est store coal1u
reghdfe d_qcoal treat did $reglist [aweight=weight_cateu] if rural==0, absorb($absorblisturbanDID ) vce(cluster clustervar)
est store coal2u
reghdfe lq_coal_hat2 treat did $reglist [aweight=weight_cateu_coal] if rural==0, absorb($absorblisturbanDID ) vce(cluster clustervar)
est store coal3u

reghdfe lexp_lpg treat did $reglist [aweight=weight_cateu] if rural==0, absorb($absorblisturbanDID ) vce(cluster clustervar)
est store lpg1u
reghdfe d_elpg treat did $reglist [aweight=weight_cateu] if rural==0, absorb($absorblisturbanDID ) vce(cluster clustervar)
est store lpg2u
reghdfe lexp_lpg2 treat did $reglist [aweight=weight_cateu_lpg] if rural==0, absorb($absorblisturbanDID ) vce(cluster clustervar)
est store lpg3u


*** tables
local filename table_a1_robss_did_cattu
est restore main1u
outreg2 using "$output\\`filename'.tex", replace tex(fragment) label dec(3) adjr2
est restore main2u
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore main3u
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore coal1u
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore coal2u
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore coal3u
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore lpg1u
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore lpg2u
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore lpg3u
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2



****************************************
*** Table A3: Anticipation effects ************************************************************

use "$datain\glss_DiDsample.dta", clear
keep if cooking>=1 & cooking<=3
keep if (glss==6 & (aftercategory==2 | aftercategory==3)) | (glss==5 & eligible5==1)
keep if rural==0 & anticipation==1 //exclude rural areas: too few obs

*** weights
//main
gen tag=0
levelsof location, local(temp)
foreach k of local temp{
	sum treat if location==`k'
	replace tag=1 if location==`k' & (r(mean)==1 | r(mean)==0)
}

ebalance treat $xlisturban if tag==0 & glss==5, keep(tempbalance) replace 
gen weight_cateu=_webal
ebalance treat $xlisturban if tag==0 & glss==6, keep(tempbalance) replace 
replace weight_cateu=_webal if glss==6

//coal intensive
cap drop tag2
gen tag2=0
levelsof location, local(temp)
foreach k of local temp{
	foreach l in 0 1{
		foreach t in 0 1{
			qui sum treat if location==`k' & d_road==`l' & elec==`t' & lq_coal_hat2!=.
			qui replace tag2=1 if location==`k' & d_road==`l' & elec==`t' & lq_coal_hat2!=. & (r(mean)==1 | r(mean)==0)
		}
	}
}
ebalance treat $xlist if tag2==0 & glss==5 & lq_coal_hat2!=., keep(tempbalance) replace 
gen weight_cateu_coal=_webal
ebalance treat $xlist if tag2==0 & glss==6 & lq_coal_hat2!=., keep(tempbalance) replace 
replace weight_cateu_coal=_webal if glss==6

//lpg intensive
cap drop tag3
gen tag3=0
levelsof location, local(temp)
foreach k of local temp{
	foreach l in 0 1{
		foreach t in 0 1{
			qui sum treat if location==`k' & d_road==`l' & elec==`t' & lexp_lpg2!=.
			qui replace tag3=1 if location==`k' & d_road==`l' & elec==`t' & lexp_lpg2!=. & (r(mean)==1 | r(mean)==0)
		}
	}
}
ebalance treat $xlist if tag3==0 & glss==5 & lexp_lpg2!=., keep(tempbalance) replace 
gen weight_cateu_lpg=_webal
ebalance treat $xlist if tag3==0 & glss==6 & lexp_lpg2!=., keep(tempbalance) replace 
replace weight_cateu_lpg=_webal if glss==6


*** regressions
reghdfe lq_coal_hat treat did $reglist [aweight=weight_cateu] if rural==0, absorb($absorblisturbanDID ) vce(cluster clustervar)
est store coal1u
reghdfe d_qcoal treat did $reglist [aweight=weight_cateu] if rural==0, absorb($absorblisturbanDID ) vce(cluster clustervar)
est store coal2u
reghdfe lq_coal_hat2 treat did $reglist [aweight=weight_cateu_coal] if rural==0, absorb($absorblisturbanDID ) vce(cluster clustervar)
est store coal3u

reghdfe lexp_lpg treat did $reglist [aweight=weight_cateu] if rural==0, absorb($absorblisturbanDID ) vce(cluster clustervar)
est store lpg1u
reghdfe d_elpg treat did $reglist [aweight=weight_cateu] if rural==0, absorb($absorblisturbanDID ) vce(cluster clustervar)
est store lpg2u
reghdfe lexp_lpg2 treat did $reglist [aweight=weight_cateu_lpg] if rural==0, absorb($absorblisturbanDID ) vce(cluster clustervar)
est store lpg3u



*** tables 
local filename table_a3_robant_did
est restore coal1u
outreg2 using "$output\\`filename'.tex", replace tex(fragment) label dec(3) adjr2
est restore coal2u
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore coal3u
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore lpg1u
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore lpg2u
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore lpg3u
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2



******************************
*** Table A2 other expenditures ************************************************************************
use "$datain\glss_DiDsample.dta", clear
cd "$output"
keep if (glss==6 & (aftercategory==2 | aftercategory==3)) | (glss==5 & eligible5==1)
keep if cooking!=0
drop if cooking==.

merge 1:1 hid using "$output\weights_eb"
drop _merge

foreach est in ate cater cateu {
	cap noisily replace weight_`est' = weight_`est'5 if glss==5
	cap noisily replace weight_`est'_coal = weight_`est'5_coal if glss==5
	cap noisily replace weight_`est'_lpg = weight_`est'5_lpg if glss==5
}


foreach var in cloth educ { //elec health
	reghdfe lexp_`var' treat did $reglist [aweight=weight_ate], absorb($absorblistDID ) vce(cluster clustervar)
	est store `var'
	reghdfe lexp_`var' treat did $reglist [aweight=weight_cater] if rural==1, absorb($absorblistDID ) vce(cluster clustervar)
	est store `var'r
	reghdfe lexp_`var' treat did $reglist [aweight=weight_cateu] if rural==0, absorb($absorblisturbanDID ) vce(cluster clustervar)
	est store `var'u
}

//tables
local filename table_a2_rob_otherexp_did
est restore educ
outreg2 using "$output\\`filename'.tex", replace tex(fragment) label dec(3) adjr2
est restore educr
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore educu
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore cloth
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore clothr
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore clothu
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2




***********************************
*** Table A4: supply shortages ************************************************************************
use "$datain\glss_DiDsample.dta", clear
cd "$output"
keep if (glss==6 & (aftercategory==2 | aftercategory==3)) | (glss==5 & eligible5==1)
keep if cooking!=0
drop if cooking==.
drop if loc7==4 //exclude urban savannah

*** weights
//main
gen tag=0
levelsof location, local(temp)
foreach k of local temp{
	sum treat if location==`k'
	replace tag=1 if location==`k' & (r(mean)==1 | r(mean)==0)
}

ebalance treat $xlisturban if tag==0 & glss==5, keep(tempbalance) replace 
gen weight_cateu=_webal
ebalance treat $xlisturban if tag==0 & glss==6, keep(tempbalance) replace 
replace weight_cateu=_webal if glss==6

//coal intensive
cap drop tag2
gen tag2=0
levelsof location, local(temp)
foreach k of local temp{
	foreach l in 0 1{
		foreach t in 0 1{
			qui sum treat if location==`k' & d_road==`l' & elec==`t' & lq_coal_hat2!=.
			qui replace tag2=1 if location==`k' & d_road==`l' & elec==`t' & lq_coal_hat2!=. & (r(mean)==1 | r(mean)==0)
		}
	}
}
ebalance treat $xlist if tag2==0 & glss==5 & lq_coal_hat2!=., keep(tempbalance) replace 
gen weight_cateu_coal=_webal
ebalance treat $xlist if tag2==0 & glss==6 & lq_coal_hat2!=., keep(tempbalance) replace 
replace weight_cateu_coal=_webal if glss==6

//lpg intensive
cap drop tag3
gen tag3=0
levelsof location, local(temp)
foreach k of local temp{
	foreach l in 0 1{
		foreach t in 0 1{
			qui sum treat if location==`k' & d_road==`l' & elec==`t' & lexp_lpg2!=.
			qui replace tag3=1 if location==`k' & d_road==`l' & elec==`t' & lexp_lpg2!=. & (r(mean)==1 | r(mean)==0)
		}
	}
}
ebalance treat $xlist if tag3==0 & glss==5 & lexp_lpg2!=., keep(tempbalance) replace 
gen weight_cateu_lpg=_webal
ebalance treat $xlist if tag3==0 & glss==6 & lexp_lpg2!=., keep(tempbalance) replace 
replace weight_cateu_lpg=_webal if glss==6



//Urban
gen didu=did
reghdfe d_wood treat didu $reglist [aweight=weight_cateu] if rural==0, absorb($absorblisturbanDID ) vce(cluster clustervar)
est store main1u
reghdfe d_charcoal treat didu $reglist [aweight=weight_cateu] if rural==0, absorb($absorblisturbanDID ) vce(cluster clustervar)
est store main2u
reghdfe d_lpg treat didu $reglist [aweight=weight_cateu] if rural==0, absorb($absorblisturbanDID ) vce(cluster clustervar)
est store main3u

reghdfe lq_coal_hat treat didu $reglist [aweight=weight_cateu] if rural==0, absorb($absorblisturbanDID ) vce(cluster clustervar)
est store coal1u
reghdfe d_qcoal treat didu $reglist [aweight=weight_cateu] if rural==0, absorb($absorblisturbanDID ) vce(cluster clustervar)
est store coal2u
reghdfe lq_coal_hat2 treat didu $reglist [aweight=weight_cateu_coal] if rural==0, absorb($absorblisturbanDID ) vce(cluster clustervar)
est store coal3u

reghdfe lexp_lpg treat didu $reglist [aweight=weight_cateu] if rural==0, absorb($absorblisturbanDID ) vce(cluster clustervar)
est store lpg1u
reghdfe d_elpg treat didu $reglist [aweight=weight_cateu] if rural==0, absorb($absorblisturbanDID ) vce(cluster clustervar)
est store lpg2u
reghdfe lexp_lpg2 treat didu $reglist [aweight=weight_cateu_lpg] if rural==0, absorb($absorblisturbanDID ) vce(cluster clustervar)
est store lpg3u


//tables
local filename table_a4_rob_supply_cattu
est restore main1u
outreg2 using "$output\\`filename'.tex", replace tex(fragment) label dec(3) adjr2
est restore main2u
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore main3u
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore coal1u
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore coal2u
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore coal3u
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore lpg1u
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore lpg2u
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore lpg3u
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2





